<?PHP
	/************************************************************************
	*       This file, in addition to all other parts of X7 Chat are        *
	*                   Copyright 2003-2007 by Tim Chamness.                *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* Version:          X7 Chat 3.0.0-A2                                    *
	* Release Date:     October 20, 2007                                    *
	* Author:           Tim Chamness                                        *
	* Copyright:        2003-2007 by Tim Chamness                           *
	* Website:          http://www.x7chat.com/                              *
	* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
	* X7 Chat 3 is free software; you can redistribute it and/or modify     *
	* it under the terms of the GNU General Public License as published by  *
	* the Free Software Foundation; either version 3 of the License, or     *
	* (at your option) any later version.                                   *
	*                                                                       *
	* X7 Chat 3 is distributed in the hope that it will be useful, but      *
	* WITHOUT ANY WARRANTY; without even the implied warranty of            *
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
	* General Public License for more details.                              *
	*                                                                       *
	* You should have received a copy of the GNU General Public License     *
	* along with this program.  If not, see <http://www.gnu.org/licenses/>. *
	*                                                                       *
	* Additional Terms, compatible with Section 7 of the GNU GPL v3,        *
	* apply to X7 Chat 3 and all X7 Chat 3 files, including this one.       *
	* You should have received a copy of these Additional Terms in the      *
	* file docs/COPYING.ADDITIONAL_TERMS.  If you did not receive a copy    *
	* please visit <http://www.x7chat.com/COPYING.ADDITIONAL_TERMS/>.       *
	************************************************************************/

	// Make sure this file isn't being accessed directly
	if(!defined('IN_X7CHAT3')) return;
	
	$section = lib_input::read('section','settings');
	$GLOBALS['OUTPUT']->cpsection = $section;
	$RID = x7in::read('rid','i');
	$ROOM = new lib_room($RID);
	
	$valid_sections = array('settings'=>'EDIT_ROOM_SETTINGS',
				'access'=>'EDIT_ROOM_ACCESS',
				'accessperms'=>'EDIT_ROOM_ACCESS',
				'banlist'=>'BAN_KICK_USER',
				'permissions'=>'EDIT_ROOM_PERMS',
				'mutelist'=>'MUTE_USER',
				'voicectrls'=>'MUTE_USER',
				'voicelist'=>'MUTE_USER',
				'logs'=>'SET_ROOM_LOG');
	
	if(!$ROOM->is_valid() || (isset($valid_sections[$section]) && !$ROOM->has_permission($ses->user,$valid_sections[$section])))
		trigger_error($GLOBALS['LANG']->lang_global->not_authorised);
		
	$GLOBALS['OUTPUT']->cpmenu = array();

	if($ROOM->has_permission($ses->user,'EDIT_ROOM_SETTINGS'))
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->settings][$GLOBALS['LANG']->lang_roomcp->general_options] = 'settings';

	if($ROOM->has_permission($ses->user,'EDIT_ROOM_PERMS'))
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->settings][$GLOBALS['LANG']->lang_roomcp->user_group_permissions] = 'permissions';

	if($ROOM->has_permission($ses->user,'SET_ROOM_LOG'))
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->settings][$GLOBALS['LANG']->lang_roomcp->logs] = 'logs';

	if($ROOM->has_permission($ses->user,'EDIT_ROOM_ACCESS')){
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->access_ctrls][$GLOBALS['LANG']->lang_roomcp->access_mode] = 'access';
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->access_ctrls][$GLOBALS['LANG']->lang_roomcp->access_perms] = 'accessperms';
	}

	if($ROOM->has_permission($ses->user,'BAN_KICK_USER')){
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->access_ctrls][$GLOBALS['LANG']->lang_roomcp->banlist] = 'banlist';
	}
	
	if($ROOM->has_permission($ses->user,'MUTE_USER')){
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->speaker_ctrls][$GLOBALS['LANG']->lang_roomcp->speaker_mode] = 'voicectrls';
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->speaker_ctrls][$GLOBALS['LANG']->lang_roomcp->speaker_perms] = 'voicelist';
		$GLOBALS['OUTPUT']->cpmenu[$GLOBALS['LANG']->lang_roomcp->speaker_ctrls][$GLOBALS['LANG']->lang_roomcp->mutelist] = 'mutelist';
	}

	$GLOBALS['OUTPUT']->rid = $RID;
	$GLOBALS['OUTPUT']->rname = $ROOM->data_cache['name'];
	
	switch($section){
		case 'settings':
			// Require permissions: EDIT_ROOM_SETTINGS
			$form = new x7form();
	
			$form->add_section(1,$GLOBALS['LANG']->lang_roomcp->settings);
			$form->add_field_text(1,'room_name',$GLOBALS['LANG']->lang_roommgmt->room_name,$ROOM->data_cache['name']);
			$form->add_field_text(1,'topic',$GLOBALS['LANG']->lang_roommgmt->topic,$ROOM->data_cache['topic'],lib_input::ALLOWALL,$GLOBALS['LANG']->lang_roommgmt->topic_desc);
			$form->add_field_text(1,'greeting',$GLOBALS['LANG']->lang_roommgmt->greeting,$ROOM->data_cache['greeting'],lib_input::ALLOWALL,$GLOBALS['LANG']->lang_roommgmt->greeting_desc);
			$form->add_field_text(1,'max_users',$GLOBALS['LANG']->lang_roommgmt->max_users,$ROOM->data_cache['max_users'],lib_input::NUMBER);
	
			$res = $form->get_results();
			if($res){
				
				$len = x7utf8::strlen($res['room_name']);
				if($len < $settings['roomname_min_length'])
					$form->add_field_error('room_name',$GLOBALS['LANG']->lang_roommgmt->too_short_roomname);
				elseif($len > $settings['roomname_max_length'])
					$form->add_field_error('room_name',$GLOBALS['LANG']->lang_roommgmt->too_long_roomname);
				elseif($res['room_name'] != $ROOM->data_cache['name']){
					$ROOM = new lib_room($res['room_name'],'name');
					if($ROOM->is_valid())
						$form->add_field_error('room_name',$GLOBALS['LANG']->lang_roommgmt->roomname_in_use);
				}
				
				if($res['max_users'] < 3)
					$res['max_users'] = 3;
					
				if(!$form->has_errors()){
					
					$ROOM->data_cache['name'] = $res['room_name'];
					$ROOM->data_cache['topic'] = $res['topic'];
					$ROOM->data_cache['greeting'] = $res['greeting'];
					$ROOM->data_cache['max_users'] = $res['max_users'];
					
					$ROOM->commit_changes();
					
				}
				
			}
			
			echo tpl_roomcp::settings_form($form);
			return;
		break;
		case 'voicectrls':
			
			$cmode = x7in::read('speaker_mode');
			if(!empty($cmode)){
				
				if($cmode == 1){
					$ROOM->set_permission('SEND_REGULAR_MESSAGE',1);
				}else{
					$ROOM->set_permission('SEND_REGULAR_MESSAGE',0);
				}
				
				$ROOM->commit_changes();
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_roomcp->speaker_mode_changed;
			}
			
			$GLOBALS['OUTPUT']->current_mode = (bool)($ROOM->local_permission_value(null,'SEND_REGULAR_MESSAGE') == 1);
			$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->speaker_mode;
			echo tpl_roomcp::voicectrls();
		break;
		case 'voicelist':

			$message = '';
			
			$type = x7in::read('type');
			$perm = x7in::read('perm');
			$rem = x7in::read('tochange');
			if(!empty($type) && !empty($perm) && !empty($rem)){
				if(lib_room_extended::set_permissions_mode($ROOM,$rem,$type,$perm,true) !== true)
					$GLOBALS['OUTPUT']->message = ($type == 'group' ? $GLOBALS['LANG']->lang_roomcp->group_not_found : $GLOBALS['LANG']->lang_roomcp->user_not_found);
			}
			
			$units = array('group'=>array('moderators'=>array(),'voiced'=>array()),'user'=>array('moderators'=>array(),'voiced'=>array()));
			foreach($units as $key=>$val){
				
				foreach($ROOM->data_cache['perms'][$key] as $id=>$parray){
					$g = ($key == 'group') ? new lib_group($id) : new lib_user($id);
					if(!$g->is_valid())
						continue;
						
					if($ROOM->local_permission_value($g,'SEND_REGULAR_MESSAGE') == 1){
						if($ROOM->local_permission_value($g,'READ_UNVOICED_USERS') == 1){
							$units[$key]['moderators'][] = array('id'=>$id,'name'=>$g->data_cache['name']);
						}else{
							$units[$key]['voiced'][] = array('id'=>$id,'name'=>$g->data_cache['name']);
						}
					}
				}
			}
			$GLOBALS['OUTPUT']->units = $units;
			$GLOBALS['OUTPUT']->warning = (bool)($ROOM->local_permission_value(null,'SEND_REGULAR_MESSAGE') == 1);
			$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->speaker_perms;
			
			echo tpl_roomcp::voicelist();

		break;
		case 'mutelist':

			$message = '';
			
			$type = x7in::read('type');
			if(!empty($type)){
				$add = x7in::read('add');
				$rem = x7in::read('rem');
				
				if(!empty($add) && lib_room_extended::set_permissions_mode($ROOM,$add,$type,'mute',true) !== true)
					$GLOBALS['OUTPUT']->message = ($type == 'group' ? $GLOBALS['LANG']->lang_roomcp->group_not_found : $GLOBALS['LANG']->lang_roomcp->user_not_found);
				
				if(!empty($rem))
					lib_room_extended::set_permissions_mode($ROOM,$rem,$type,'unmute',true);
			}
			
			$units = array('group'=>array(),'user'=>array());
			foreach($units as $key=>$val){
				
				foreach($ROOM->data_cache['perms'][$key] as $id=>$perms){
					$g = ($key == 'group') ? new lib_group($id) : new lib_user($id);
					if(!$g->is_valid())
						continue;
						
					if($ROOM->local_permission_value($g,'SEND_REGULAR_MESSAGE') == -1)
						$units[$key][] = array('id'=>$id,'name'=>$g->data_cache['name']);
				}
				
			}
			
			$GLOBALS['OUTPUT']->units = $units;
			$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->mutelist;
			
			echo tpl_roomcp::mutelist();
		break;
		case 'access':
			// Require permissions: EDIT_ROOM_ACCESS

			if($ROOM->local_permission_value(null,'PASSWORD_CORRECT') == 1)
				$GLOBALS['OUTPUT']->default_mode = 1;
			elseif(!empty($ROOM->data_cache['password']))
				$GLOBALS['OUTPUT']->default_mode = 2;
			else
				$GLOBALS['OUTPUT']->default_mode = 3;

			$amc = x7in::read('access_mod');
			if(!empty($amc)){
				// Reset chatroom to permissions access mode
				switch($amc){
					case 3:
						$ROOM->set_permission('PASSWORD_CORRECT',0);
						$ROOM->data_cache['password'] = '';
						$GLOBALS['OUTPUT']->default_mode = 3;
					break;
					case 2:
						$ROOM->set_permission('PASSWORD_CORRECT',0);
						$ROOM->data_cache['password'] = x7in::read('password');
						$GLOBALS['OUTPUT']->default_mode = 2;
						if(!empty($ROOM->data_cache['password']))
							break;
					default:
						$ROOM->set_permission('PASSWORD_CORRECT',1);
						$ROOM->data_cache['password'] = '';
						$GLOBALS['OUTPUT']->default_mode = 1;
					break;
				}

				$ROOM->commit_changes();
				$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_roomcp->access_mod_updated;
			}

			$GLOBALS['OUTPUT']->default_pass = $ROOM->data_cache['password'];
			$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->access_mode;
			
			echo tpl_roomcp::access_mode();
		break;
		case 'accessperms':

			$changes_made = false;
			
			$type = x7in::read('type');
			if(!empty($type) && ($type == 'user' || $type == 'group')){
				$add = x7in::read('add');
				$rem = x7in::read('rem');
				
				if(!empty($add)){
					$test = ($type == 'user') ? new lib_user($add) : new lib_group($add);
					if($test->is_valid()){
						$changes_made = true;
						$ROOM->set_permission('PASSWORD_CORRECT',1,$test);
					}else
						$GLOBALS['OUTPUT']->message .= $GLOBALS['OUTPUT']->lang_roomcp->{$type.'_not_found'}."<br /><br />";
				}
				
				if(!empty($rem)){
					$test = ($type == 'user') ? new lib_user($rem) : new lib_group($rem);
					$changes_made = true;
					$ROOM->set_permission('PASSWORD_CORRECT',0,$test);
				}
			
			}
			
			$units = array('group'=>array(),'user'=>array());
			foreach($units as $key=>$val){
				
				foreach($ROOM->data_cache['perms'][$key] as $id=>$pgroup_array){
					$g = ($key == 'group') ? new lib_group($id) : new lib_user($id);
					
					if($g->is_valid() && $ROOM->local_permission_value($g,'PASSWORD_CORRECT') == 1){
						$units[$key][] = array('id'=>$id,'name'=>$g->data_cache['name']);
					}
				}
				
			}
			

			if($changes_made)
				$ROOM->commit_changes();

			$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->access_perms;
			$GLOBALS['OUTPUT']->units = $units;
			$GLOBALS['OUTPUT']->warning = (bool)($ROOM->local_permission_value(null,'PASSWORD_CORRECT') == 1);
			
			echo tpl_roomcp::access_perms();
		break;
		case 'banlist':
			// Require permissions: BAN_KICK_USER
			
			$ban = x7in::read('name');
			if(!empty($ban)){
				if(($error = lib_room_extended::ban_user($ROOM,$ban,(int)($_POST['length_unit']*$_POST['length']))) !== true)
					$GLOBALS['OUTPUT']->message = $GLOBALS['LANG']->lang_roomcp->$error;	
			}
			
			$rem = x7in::read('rem','i');
			if(!empty($rem)){
				lib_room_extended::set_permissions_mode($ROOM,$rem,'user','unban');
			}
			
			$bans = array();
			foreach($ROOM->data_cache['ban_data'] as $uid=>$end){
				$user = new lib_user(str_replace('u','',$uid));
				if($user->is_valid() && ($end == 0 || $end > $ses->time)){
					$remains = $end-$ses->time;
					if($end == 0)
						$end = $GLOBALS['LANG']->lang_roomcp->forever;
					elseif($remains > 86400)
						$end = round($remains/86400,2) . ' ' . $GLOBALS['LANG']->lang_roomcp->days;
					else
						$end = round($remains/3600,2) . ' ' . $GLOBALS['LANG']->lang_roomcp->hours;
					
					$bans[] = array_merge($user->data_cache,array('end'=>$end));
				}
				
			}
			
			$form = new form();
			$form->add_field('text','name',$GLOBALS['LANG']->lang_roomcp->username);
			$form->add_field('text','length',$GLOBALS['LANG']->lang_roomcp->length);
			$form->add_field('select','length_unit',$GLOBALS['LANG']->lang_roomcp->length_unit,'',array($GLOBALS['LANG']->lang_roomcp->forever=>0,$GLOBALS['LANG']->lang_roomcp->hours=>3600,$GLOBALS['LANG']->lang_roomcp->days=>3600*24));
			
			$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->banlist;
			$GLOBALS['OUTPUT']->bans = $bans;
			$GLOBALS['OUTPUT']->ban_form = $form->get_fields();
			
			echo tpl_roomcp::banlist();
		break;
		case 'permissions':
			// Require permissions: EDIT_ROOM_PERMS
			
			$changes_made = false;
			
			$add = x7in::read('add');
			$type = x7in::read('type');
			$edit = x7in::read('edit','i');
			$doedit = x7in::read('doedit','i');
			
			if(!empty($add) && $type == 'user'){
				$check = new lib_user($add);
				if($check->is_valid()){
					$edit = $check->data_cache['uid'];
				}else{
					$message = $GLOBALS['LANG']->lang_roomcp->user_not_found;
					$edit = '';
				}
			}elseif(!empty($add) && $type == 'group'){
				$check = new lib_group($add);
				if($check->is_valid()){
					$edit = $check->data_cache['gid'];
				}else{
					$message = $GLOBALS['LANG']->lang_roomcp->group_not_found;
					$edit = '';
				}
			}
			
			if(!empty($type)){
				
				$valid_perms = array('ENTER_FULL_ROOM','BAN_IMMUNITY','VIEW_ROOM_LOG','SET_ROOM_LOG','BAN_KICK_USER','MUTE_USER','VIEW_IP','BECOME_INVISIBLE','SEE_INVISIBLE','EDIT_ROOM_PERMS','EDIT_ROOM_ACCESS','EDIT_ROOM_SETTINGS');
					
				if(!empty($edit)){
					if($type == 'user')
						$object = new lib_user($edit);
					else
						$object = new lib_group($edit);
					
					$form = new form();
					
					foreach($valid_perms as $perm){
						$form->add_field('select',$perm,$GLOBALS['LANG']->lang_roomcp->{'perm_'.$perm},'',array($GLOBALS['LANG']->lang_roomcp->allow=>1,$GLOBALS['LANG']->lang_roomcp->inherit=>0,$GLOBALS['LANG']->lang_roomcp->deny=>-1));
						
						$form->add_default($perm,$ROOM->local_permission_value($object,$perm));
					}
					
					$GLOBALS['OUTPUT']->edittype = $type;
					$GLOBALS['OUTPUT']->toedit = $edit;
					$GLOBALS['OUTPUT']->form = $form->get_fields();
					$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->user_group_permissions;
					
					echo tpl_roomcp::permissions_form();
					return;
				}
				
				if(!empty($doedit)){
					$editobject = ($type == 'user') ? new lib_user($doedit) : new lib_group($doedit);
					
					foreach($valid_perms as $perm){
						$ROOM->set_permission($perm,(int)$_POST[$perm],$editobject);
					}
					
					$changes_made = true;
				}
			
			}
			
			// Search for users with special permissions
			$gunits = array();
			foreach($ROOM->data_cache['perms']['group'] as $gid=>$perms){
				$g = new lib_group($gid);
				if(!$g->is_valid()){
					// wtf a bad group? remove for security reasons
					$changes_made = true;
					unset($ROOM->data_cache['perms']['group'][$gid]);
				}else
					$gunits[] = array('id'=>$gid,'name'=>$g->data_cache['name']);
			}

			$uunits = array();
			foreach($ROOM->data_cache['perms']['user'] as $uid=>$perms){
				$g = new lib_user($uid);
				if(!$g->is_valid()){
					// wtf a bad user? remove for security reasons
					$changes_made = true;
					unset($ROOM->data_cache['perms']['user'][$uid]);
				}else
					$uunits[] = array('id'=>$uid,'name'=>$g->data_cache['name']);
			}
			
			if($changes_made)
				$ROOM->commit_changes();
			
			$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->user_group_permissions;
			$GLOBALS['OUTPUT']->units = array('user'=>$uunits,'group'=>$gunits);
				
			echo tpl_roomcp::permissions();
		break;
		case 'logs':
			// Require permissions: SET_ROOM_LOG
			
			$change_state = x7in::read('state','i');
			if(!empty($change_state)){
				lib_logging::cache_event("messages/{$ROOM->data_cache['rid']}");
				$ROOM->data_cache['log'] = ($change_state == 1);
				$ROOM->commit_changes();
			}
			
			if(isset($_GET['del'])){
				lib_logging::cache_event("messages/{$ROOM->data_cache['rid']}");
				if(is_file("./data/log_text/r{$ROOM->data_cache['rid']}.php"))
					unlink("./data/log_text/r{$ROOM->data_cache['rid']}.php");
			}
			
			$GLOBALS['OUTPUT']->section = $GLOBALS['LANG']->lang_roomcp->logs;
			$GLOBALS['OUTPUT']->logging_on = $ROOM->data_cache['log'];
			echo tpl_roomcp::logs();
		break;
		default:
			$GLOBALS['OUTPUT']->section = '';
			echo tpl_roomcp::main();
		break;
	}

?>